var flag = false;
var getFlag = false;
var loadingFlag = false;
var redId = 0;
$(function() {
	var begin = new Date();
	$.ajax({
		url : appPath + '/red/getTime.json',
		dataType : 'json',
		type : 'post',
		success : function(data) {
			if (data.time2 == null||data.time2 == undefined) {
				$('#next1').hide();
				$('#next2').show();
			} else {
				$('#next2').hide();
				$('#next1').show();
			}
			redId = data.redId;
			if (data.time1 != null && data.time1 != undefined) {
				timer1(Math.round(data.time1
						- (new Date().valueOf() - begin.valueOf() - data.com))/1000);
			}else{
				$('#begin1').hide();
				$('#begin2').hide();
				$('#begin3').hide();
				$('#begin4').show();
			}
			if (data.time2 != null && data.time2 != undefined) {
				timer2(Math.round(data.time2
						- (new Date().valueOf() - begin.valueOf() - data.com))/1000);
			}
		},
		error : function() {
			$.alert("系统异常，请重试");
		}
	})

	window.setInterval(function() {
		flag = false
	}, 2000);
});
function timer1(intDiff) {
	var t1 = setInterval(function() {
		var day = 0, hour = 0, minute = 0, second = 0;// 时间默认值
		if (intDiff > 0) {
			hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
			minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
			second = Math.floor(intDiff) - (day * 24 * 60 * 60)
					- (hour * 60 * 60) - (minute * 60);
		} else {
			$('#begin1').hide();
			$('#begin2').show();
			$('#begin3').hide();
			$('#begin4').hide();
			clearTimeout(t1);
		}
		if (minute <= 9)
			minute = '0' + minute;
		if (second <= 9)
			second = '0' + second;

		$('#hour_show1').html('<s id="h"></s>' + hour + '时');
		$('#minute_show1').html('<s></s>' + minute + '分');
		$('#second_show1').html('<s></s>' + second + '秒');
		intDiff--;
	}, 1000);
}
function timer2(intDiff) {
	var t2 = setInterval(function() {
		var day = 0, hour = 0, minute = 0, second = 0;// 时间默认值
		if (intDiff > 0) {
			hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
			minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
			second = Math.floor(intDiff) - (day * 24 * 60 * 60)
					- (hour * 60 * 60) - (minute * 60);
		} else {
			clearTimeout(t2);
		}

		if (minute <= 9)
			minute = '0' + minute;
		if (second <= 9)
			second = '0' + second;
		$('#hour_show2').html('<s id="h"></s>' + hour + '时');
		$('#minute_show2').html('<s></s>' + minute + '分');
		$('#second_show2').html('<s></s>' + second + '秒');
		intDiff--;
	}, 1000);

}

// 我要升级取消订单
function check() {
	if (getFlag) {
		$.alert("您已抢过了");
		return;
	}
	if (loadingFlag) {
		return;
	}
	if (flag) {
		return;
	}
	flag = true;
	loadingFlag = true;
	$("#wrong").text("");
	$.ajax({
		url : appPath + '/red/check.json',
		dataType : 'json',
		type : 'post',
		data : {
			token : $.cookie("token"),
			redId : redId
		},
		success : function(data) {
			loadingFlag = false;
			
			if (data.code == -1) {
			$
			.modal({
				title : "未登录",
				text : '请先登录',
				buttons : [ {
					text : "登录",
					onClick : function() {
						gotoHtml('/member/login.html');
					}
				}, {
					text : "注册",
					onClick : function() {
						gotoHtml('/member/register.html');
					}
				} , {
					text : "取消"
				}]
			});
			}
			
			
			if (data.code == 2) {
				$.alert(data.msg, function() {
					window.location.reload();
				});
				return;
			}
			if (data.code == 3) {
				$.alert(data.msg);
				$('#begin1').hide();
				$('#begin2').hide();
				$('#begin3').show();
				$('#begin4').hide();
				return;
			}
			if (data.code == 0) {
				$("#wrong").text(data.msg);
				return;
			}
			if (data.code == 1) {
				getFlag = true;
				gotoHtml('/red/success.html');
				return;
			}
		},
		error : function() {
			loadingFlag = false;
			$.alert("系统异常，请重试");
		}
	})

}
